package com.shizhanzhe.zxbapp.home;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.shizhanzhe.zxbapp.R;
import com.shizhanzhe.zxbapp.bean.MeterialBean;
import com.shizhanzhe.zxbapp.bean.PDDSC;
import com.shizhanzhe.zxbapp.bean.UserBean;
import com.shizhanzhe.zxbapp.my.ShareCodeActivity;
import com.shizhanzhe.zxbapp.url.Path;
import com.shizhanzhe.zxbapp.utils.OkHttpDownloadJsonUtil;
import com.shizhanzhe.zxbapp.utils.Util;
import com.shizhanzhe.zxbapp.widget.CircleImageView;

import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SourceMaterialActivity extends AppCompatActivity {

    @BindView(R.id.top_title)
    TextView topTitle;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    ;
    @BindView(R.id.iv_head)
    CircleImageView ivHead;
    @BindView(R.id.tv_nc)
    TextView tvNc;
    @BindView(R.id.userid)
    TextView userid;
    @BindView(R.id.iv_ewm)
    ImageView ivEwm;
    @BindView(R.id.rl)
    RelativeLayout rl;
    @BindView(R.id.refresh)
    SmartRefreshLayout refresh;
    private SourceAdapter mAdapter;
    private PDDSourceAdapter mpddAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_source_metarial);
        ButterKnife.bind(this);
        ImmersionBar.with(this)
                .fitsSystemWindows(true)  //使用该属性,必须指定状态栏颜色
                .statusBarColor(R.color.colorAccent)
                .statusBarDarkFont(true, 0.2f)
                .init();
        topTitle.setText("素材");
        initData();

    }

    private void initData() {
        refresh.setRefreshHeader(new MaterialHeader(this));
        refresh.setRefreshFooter(new ClassicsFooter(this));
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page=1;
                sourceList();
                refreshLayout.finishRefresh(2000);
            }
        });
        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                sourceList();
                refreshLayout.finishLoadMore(2000);
            }
        });
        recycler.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new SourceAdapter(this);
        mpddAdapter = new PDDSourceAdapter(this);
        userid.setText("  ID:" + UserBean.uid + "  ");
        tvNc.setText("".equals(UserBean.nickname) ? "赚小宝" : UserBean.nickname);
        Glide.with(this).load(Path.BASE_URL + UserBean.headimg)
                .apply(new RequestOptions().error(R.mipmap.head)).into(ivHead);
        ivEwm.setImageBitmap(Util.encodeAsBitmap(Path.zxb_register(UserBean.ip,UserBean.uid)));
        setListener();
        sourceList();
    }

    private void setListener() {
        findViewById(R.id.top_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mAdapter.setListener(new SourceAdapter.SourceListener() {
            @Override
            public void copyText(String text) {
                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                cm.setText(text);
                Toast.makeText(SourceMaterialActivity.this, "复制成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void shareEwm() {
                startActivity(new Intent(SourceMaterialActivity.this, ShareCodeActivity.class));
            }

            @Override
            public void saveTI(MeterialBean.SucaiBean.DataBean item) {
                //保存图文
                Util.GetImageInputStream(SourceMaterialActivity.this, Arrays.asList(item.getImg().split(",")));
                Util.saveBitmap(SourceMaterialActivity.this, getBitmapByView(rl), System.currentTimeMillis() + ".jpg");
                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                cm.setText(item.getContent());
                Toast.makeText(SourceMaterialActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void extension(MeterialBean.SucaiBean.DataBean item) {
                //一键推广
            }
        });
        mpddAdapter.setListener(new PDDSourceAdapter.PDDSourceListener() {
            @Override
            public void copyText(String text) {
                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                cm.setText(text);
                Toast.makeText(SourceMaterialActivity.this, "复制成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void shareEwm() {
                startActivity(new Intent(SourceMaterialActivity.this, ShareCodeActivity.class));
            }

            @Override
            public void saveTI(PDDSC.DataBean item) {
                //保存图文
                Util.GetImageInputStream(SourceMaterialActivity.this, Arrays.asList(item.getImg().substring(0, item.getImg().length()).split(",")));
                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                cm.setText(item.getContent());
                Toast.makeText(SourceMaterialActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void extension(PDDSC.DataBean item) {
                //一键推广
            }
        });
        recycler.setAdapter(mAdapter);
    }

    private Bitmap getBitmapByView(View headerView) {
        int h = headerView.getHeight();
        Bitmap bitmap = Bitmap.createBitmap(headerView.getWidth(), h, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        headerView.draw(canvas);
        return bitmap;
    }

    int page = 1;

    private void sourceList() {

        if (getIntent().getIntExtra("type", 0) == 1) {

            OkHttpDownloadJsonUtil.downloadJson(SourceMaterialActivity.this, Path.material + page, new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {

                @Override
                public void onsendJson(String json) {
                    if (!"1".equals(json) && !"2".equals(json)) {
                        Gson gson = new Gson();
                        List<MeterialBean.SucaiBean.DataBean> tiBean = gson.fromJson(json, MeterialBean.class).getSucai().getData();
                        if (tiBean.size() > 0) {
                            mAdapter.updateData(tiBean, page);
                        } else {
                            Toast.makeText(SourceMaterialActivity.this, "暂无邀请素材", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(SourceMaterialActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        } else if (getIntent().getIntExtra("type", 0) == 2) {
            recycler.setAdapter(mpddAdapter);
            OkHttpDownloadJsonUtil.downloadJson(SourceMaterialActivity.this, Path.pddmaterial, new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {

                @Override
                public void onsendJson(String json) {
                    if (!"1".equals(json) && !"2".equals(json)) {
                        Gson gson = new Gson();
                        PDDSC tiBean = gson.fromJson(json, PDDSC.class);
                        if (tiBean.getData().size() > 0) {
                            mpddAdapter.updateData(tiBean.getData(), 1);
                        } else {
                            Toast.makeText(SourceMaterialActivity.this, "暂无商品素材", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(SourceMaterialActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }
}
